package com.example.androidframework.ui.privacy

import android.annotation.SuppressLint
import android.content.Context
import android.text.Html
import android.view.View
import android.webkit.WebView
import android.widget.TextView
import com.example.androidframework.R
import com.example.androidframework.ui.main.MainActivity
import com.lxj.xpopup.core.CenterPopupView
import com.martin.lib_base.LibBase
import com.martin.lib_base.ktx.buildRequest
import com.martin.lib_base.ktx.launchCollect
import com.martin.lib_base.ktx.loge
import com.martin.lib_base.net.NetApply

@SuppressLint("ViewConstructor")
class UserPrivacyDetailPopView(
    context: Context,
    val type: Int = 1,
    val title: String = "",
    val onCanel: () -> Unit?,
    val onSubmit: () -> Unit?,
) : CenterPopupView(context) {

    lateinit var webView: WebView
    lateinit var contentTv: TextView

    override fun getImplLayoutId(): Int {
        return R.layout.pop_view_user_privacy
    }

    @SuppressLint("SetJavaScriptEnabled")
    override fun onCreate() {
        super.onCreate()

        findViewById<TextView>(R.id.title).text = title
        findViewById<TextView>(R.id.hint).visibility = View.INVISIBLE

        webView = findViewById<WebView>(R.id.content_tv)

        contentTv = findViewById<TextView>(R.id.content)

        val settings = webView?.settings
        settings!!.javaScriptEnabled = true

        findViewById<TextView>(R.id.cancel).setOnClickListener {
            onCanel.invoke()
            dismiss()
        }

        findViewById<TextView>(R.id.submit).setOnClickListener {
            onSubmit.invoke()
            dismiss()
        }

        getContent()

    }

    override fun getMaxWidth(): Int {
        return resources.displayMetrics.widthPixels - 140
    }

    private fun getContent() {
        launchCollect({
            NetApply.api.configArticleDetails(
                buildRequest(
                    "service", "Config.ArticleDetails", mapOf("article_id" to type)
                )
            )
        }) {
            onSuccess = { data ->
                data.info?.let {
                    // contentTv?.text=Html.fromHtml(it.postContent)
                    webView.loadDataWithBaseURL(
                        "",
                        Html.fromHtml(it.postContent).toString(),
                        "text/html",
                        "UTF-8",
                        null
                    )
                }
            }
        }

    }

}